<template>
  <div class="addOwner">
    <el-form :model="formData" ref="ruleForm" :rules="rules" label-width="68px">
      <el-form-item label="姓名:" prop="username">
        <el-input v-model="formData.username" size="mini"></el-input>
      </el-form-item>

      <el-form-item label="电话:" prop="telephone">
        <el-input v-model="formData.telephone" size="mini"></el-input>
      </el-form-item>

      <el-form-item style="text-align: center;margin-left: -30px;">
        <el-button size="mini" type="primary" @click="inspectPhone('ruleForm')">确定</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  export default {
    name: 'addOwner',
    data() {
      var validatorPhone = function (rule, value, callback) {
        if (value == undefined) {
          callback(new Error('手机号不能为空'))
        } else if (!/^1\d{10}$/.test(value)) {
          callback(new Error('手机号格式错误'))
        } else {
          callback()
        }
      }
      return {
        rules: {
          username: [{ required: true, message: '请输入商户姓名', trigger: 'blur' }],
          telephone: [{ required: true, validator: validatorPhone, trigger: 'blur' }]
        },
        formData: {},
      }
    },
    mounted() {

    },
    methods: {
      inspectPhone(ruleForm) {
        this.$axios({
          method: 'get',
          url: '/shop/shop-manager/get-by-telephone',
          params: { telephone : this.formData.telephone }
        }).then((res) => {
          if(res.data == null) {
            this.submit(ruleForm)
          } else {
            this.$message.warning('该手机号已注册');
          }
        })
      },

      submit(ruleForm) {
        this.$refs[ruleForm].validate((valid) => {
          if(valid) {
              this.$axios({
                method: 'post',
                url: '/shop/shop-manager/add',
                data: this.formData
              }).then((res) => {
                if(res.code == 200) {
                  this.$message.success('新增成功');
                  this.$emit('closeDialog',false);
                  this.$emit('refresh');
                } else {
                  this.$message.warning(res.desc);
                }
              })
          } else {
            return false
          }
        })
      }
    }
  }
</script>

<style scoped>
  .el-form {
    display: inline-block;
    margin-left: 50%;
    transform: translateX(-50%);
  }

  .el-input {
    width: 200px;
  }

  .el-form-item {
    width: 255px;
  }


</style>
